$(function () {
    // $.ajax({
    //     url: "/regStu",
    //     type: "GET",
    //     dataType: "json",
    //     contentType: "application/json;charset:utf-8",
    //     success: function (data) {
    //         var $stuDiv = $("#add-result");
    //         $.each(data, function (index, ele) {
    //             var $item = $(" <div class=\"layui-col-md2\">\n" +
    //                 "            <div class=\"layui-card\">\n" +
    //                 "                <div class=\"layui-card-header\">待体检</div>\n" +
    //                 "                <div class=\"layui-card-body\">\n" +
    //                 "                    <span class=\"deptName\" style=\"width: 100%;\">"+ele.deptName+"</span></br></hr>\n" +
    //                 "                    <span class=\"stuName\">"+ele.stuName+"</span></br></hr>\n" +
    //                 "                    <span class=\"isPhy\">"+ele.isPhy+"</span>\n" +
    //                 "                    <span class=\"deptId\" style=\"display: none\">"+ele.deptId+"</span>\n" +
    //                 "                    <span class=\"stuId\" style=\"display: none\">"+ele.stuId+"</span>\n" +
    //                 "                    <button id=\"add\" class=\"layui-btn layui-btn-sm phyexam\" class=\"layui-btn\" style=\"margin-left: 25px;\">录入结果</button>\n" +
    //                 "                </div>\n" +
    //                 "            </div>\n" +
    //                 "        </div>");
    //             if (ele.isPhy == "未体检") {
    //                 $stuDiv.append($item);
    //             }
    //         });
    //     },
    //     error: function () {
    //         alert("error");
    //     }
    // });
    $("#add-result").delegate("#add", "click", function () {
        // alert("12");
        var stuId = $(this).siblings(".stuId").text();
        var stuName = $(this).siblings(".stuName").text();
        $("#pro1").val("");
        $("#pro2").val("");
        $("#pro3").val("");
        $("#pro4").val("");
        $("#pro5").val("");
        $("#pro6").val("");
        layui.use('layer', function () {
            var layer = layui.layer;
            layer.open({
                type: 1,
                title: "项目结果录入",
                skin: 'layui-layer-rim', //加上边框
                area: ['650px', '460px'], //宽高
                content: $('#form-result'),
                yes: function (index, layero) {
                    layer.close(index);
                }
            })
        });

        $("#stuname").val(stuName);
        $("#sub-result").on("click", function () {
            var result1 = $("#pro1").val();
            var result2 = $("#pro2").val();
            var result3 = $("#pro3").val();
            var result4 = $("#pro4").val();
            var result5 = $("#pro5").val();
            var result6 = $("#pro6").val();
            $.ajax({
                url: "/result2stu",
                type: "POST",
                contentType: "application/json;charset=utf-8",
                // dataType: "json",
                data: JSON.stringify({
                    "stuId": stuId,
                    "pro1Id": "1",
                    "pro2Id": "2",
                    "pro3Id": "3",
                    "pro4Id": "4",
                    "pro5Id": "5",
                    "pro6Id": "6",
                    "result1": result1,
                    "result2": result2,
                    "result3": result3,
                    "result4": result4,
                    "result5": result5,
                    "result6": result6
                }),
                success: function () {
                    alert("结果录入成功");
                    $("#welcome").empty();
                    $("#add-result").empty();
                    $("#welcome").load("phy-register.html");
                    ajaxAddResutl();
                    tableResult();
                },
                error: function () {
                    alert("结果录入失败");
                }
            });
        });
    });

    function ajaxAddResutl() {
        $.ajax({
            url: "/regStu",
            type: "GET",
            dataType: "json",
            contentType: "application/json;charset:utf-8",
            success: function (data) {
                var $stuDiv = $("#add-result");
                $stuDiv.empty();
                $.each(data, function (index, ele) {
                    var $item = $(" <div class=\"layui-col-md2\">\n" +
                        "            <div class=\"layui-card\">\n" +
                        "                <div class=\"layui-card-header\">待体检</div>\n" +
                        "                <div class=\"layui-card-body\">\n" +
                        "                    <span class=\"deptName\" style=\"width: 100%;\">" + ele.deptName + "</span></br></hr>\n" +
                        "                    <span class=\"stuName\">" + ele.stuName + "</span></br></hr>\n" +
                        "                    <span class=\"isPhy\">" + ele.isPhy + "</span>\n" +
                        "                    <span class=\"deptId\" style=\"display: none\">" + ele.deptId + "</span>\n" +
                        "                    <span class=\"stuId\" style=\"display: none\">" + ele.stuId + "</span>\n" +
                        "                    <button id=\"add\" class=\"layui-btn layui-btn-sm phyexam\" class=\"layui-btn\" style=\"margin-left: 25px;\">录入结果</button>\n" +
                        "                </div>\n" +
                        "            </div>\n" +
                        "        </div>");
                    if (ele.isPhy == "未体检") {
                        $stuDiv.append($item);
                    }
                });
            },
            error: function () {
                alert("error");
            }
        });
    }

    function tableResult() {
        layui.use('table', function () {
            var table = layui.table;
            table.render({
                elem: '#result'
                , url: '/allResult'
                , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                , limit: 5
                , page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                    layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']//自定义分页布局
                    , limits: [5, 10, 15]
                    , first: false //不显示首页
                    , last: false //不显示尾页
                }
                // , toolbar: 'default'
                , cols: [[
                    {type: 'checkbox', fixed: 'left'}
                    , {field: 'id', title: 'ID', sort: true}
                    , {field: 'num', title: '学号', sort: true}
                    , {field: 'stuName', title: '姓名'} //width 支持：数字、百分比和不填写。你还可以通过 minWidth 参数局部定义当前单元格的最小宽度，layui 2.2.1 新增
                    , {field: 'age', title: '年龄', sort: true}
                    , {field: 'sex', title: '性别'}
                    , {field: 'contact', title: '联系方式'}
                    , {field: 'deptName', title: '所属院系'}
                    , {field: 'proName', title: '项目'}
                    , {field: 'result', title: '结果', edit: 'text'}
                    // , {field: 'qualify', title: '是否合格', align: 'center'}
                    // , {field: 'classify', title: '职业', align: 'center'} //单元格内容水平居中
                    // , {field: 'experience', title: '积分', sort: true, align: 'right'} //单元格内容水平居中
                    // , {field: 'score', title: '评分', sort: true, align: 'right'}
                    , {field: 'remark', title: '备注', align: 'right'}
                    , {fixed: 'right', title: '操作', toolbar: '#editBar', width: 150}
                ]],
                id: 'result'
            });
            var $ = layui.$, active = {
                reload: function () {
                    //获取用户名
                    // var demoReload = $('#stu_search_name');
                    //执行重载
                    table.reload('result', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        //根据条件查询
                        , where: {
                            // stuName: demoReload.val()
                            key: {
                                stuName: $("#stu_search_name").val()
                            }
                        }
                    });
                }
            };
            //点击搜索按钮根据用户名称查询
            $('#search-stu').on('click',
                function () {
                    // var type = $(this).data('type');
                    // active[type] ? active[type].call(this) : '';
                    active["reload"] ? active["reload"].call(this) : '';
                });
        });
    }
});